#!/usr/bin/env python -OO
# -*- coding: utf-8 -*-

# Copyright © 2008,2009 Trust Digital Technology Laboratory, Shanghai Jiao Tong University.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author:
#   XING Dong sheng <homer.xing@gmail.com>

from __future__ import with_statement

__all__ = ['installmatlab']

import os, sys
import xdg.DesktopEntry
import softwaredependent as D
import tempfile
from softwareindependent import getArch, TempOwn, fileinsert, filecontain, notify
#安装数学工具需要用户交互

def matlabicon():
	fn='matlab.desktop'
	e = xdg.DesktopEntry.DesktopEntry( os.path.abspath(fn) )
	e.set('Encoding','UTF-8')
	e.set('Version','1.0')
	e.set('Type','Application')
	e.set('Terminal','false')
	e.set('Exec','matlab -desktop')
	e.set('Name','Matlab')
	e.set('Icon','deskbar-applet')
	e.set('Categories','Development')
	e.write()
	D.execute("sudo mv %s /usr/share/applications/"%fn)

def matlabjre_sunjdk():
	"修正JRE，正常显示中文"
	fn="/usr/local/matlab/bin/matlab"
	toadd='export MATLAB_JAVA=/usr/lib/jvm/java-6-sun/jre #ubuntueasysetuper\n'
	if not filecontain(fn, toadd):
		with TempOwn(fn) as o:
				fileinsert(fn, 1, toadd)

def installmatlab():
	D.download("matlab")
	if D.execute("sudo mkdir /mnt/matlab ; sudo mount -t iso9660 -o iocharset=utf8,loop %s/%s /mnt/matlab"%(D.LOCAL, D.softwarelist["matlab"]) ):
		notify("Ubuntu Easy Setuper 说：","""开始安装 Matlab 2008a 版。
选‘Install manually without using the Internet’，
‘file installation key’ 填 11111-11111-02011-44270
选上 'Create symbolic links to MATLAB scripts in:'
装好后，程序会询问license。选"Activate manually without the Internet",
在"Enter the path to the license file:"一栏填写 /usr/local/matlab/lic_standalone.dat 。
""")
		if D.execute("sudo mkdir /usr/local/matlab; sudo cp /mnt/matlab/crack/lic_standalone.dat /usr/local/matlab/"):
			if D.execute("cd /usr/local/matlab ; sudo /mnt/matlab/install"):
				f = tempfile.NamedTemporaryFile(mode='w')
				f.write("\n"*9)
				f.flush()

				if D.execute("sudo /usr/local/matlab/install_matlab < %s >/dev/null"%f.name ):
					D.execute("sudo umount /mnt/matlab && sudo rmdir /mnt/matlab")
					matlabjre_sunjdk()
					matlabicon()
				f.close()
if __name__ == "__main__":
	installmatlab()
